// Package account @program: gateway
//@author: edte
//@create: 2021-12-29 09:27
package account

import (
	"context"
	"gateway/client"
	"gateway/config/account"
	"gateway/tools/checkform"
	"gateway/tools/security"
	"gateway/tools/user"
	"github.com/gin-gonic/gin"
	"google.golang.org/grpc"
	"net/http"
)

var conn, err = grpc.Dial(*client.Addr, grpc.WithInsecure())
var c = useraccount.NewUserServerClient(conn)

/*func GetLink() (useraccount.UserServerClient, error) {
	flag.Parse()
	// Set up a connection to the server.
	conn, err := grpc.Dial(*client.Addr, grpc.WithInsecure())
	// 如果建立连接失败则退回连接
	if err != nil {
		return nil, err
		//log.Fatalf("did not connect: %v\n", err)
	}
	//defer func(conn *grpc.ClientConn) {
	//	err := conn.Close()
	//	if err != nil {
	//		log.Fatalf(err.Error())
	//	}
	//}(conn)
	c := useraccount.NewUserServerClient(conn)
	return c, err
}*/

func UserLoginClient(userNum string, userPassword string) (h gin.H, err error) {
	/*c, err := GetLink()
	if err != nil {
		return gin.H{"status": http.StatusBadRequest, "message": "could not greet: " + err.Error()}, err
	}
	cxt, cancel := context.WithTimeout(context.Background(), time.Second)
	cxt, cancel := context.WithTimeout(context.Background(), time.Minute)
	defer cancel()*/
	r, err := c.UserLogin(context.Background(), &useraccount.LoginUserFormRequest{UserPhone: userNum, UserPassword: userPassword})
	// 登录出现问题
	if err != nil || r.Status != http.StatusOK {
		if err == nil {
			return gin.H{"status": http.StatusBadRequest}, err
		}
		return gin.H{"status": http.StatusBadRequest, "message": "could not greet: " + err.Error()}, err
		//log.Printf("could not greet: %v", err)
	}
	user2 := user.User{Id: int(r.UserInfo.UserId),
		UserPhone:   r.UserInfo.UserPhone,
		Password:    r.UserInfo.UserPassword,
		EmailNumber: r.UserInfo.Email,
		NickName:    r.UserInfo.Nickname}
	token := security.GetMytoken(user2)
	security.UserMap[user2.Id] = token
	h = gin.H{"status": r.Status, "message": token}
	return h, err
}
func UserRegisterClient(userNum string, userPd string, email string, nickname string) (h gin.H, err error) {
	err = form_check.RegisterFormCheck(userNum, userPd, email)
	if err != nil {
		return gin.H{"status": http.StatusBadRequest, "message": "could not greet: " + err.Error()}, err
	}
	/*	c, err := GetLink()
		if err != nil {
			return gin.H{"status": http.StatusBadRequest, "message": "could not greet: " + err.Error()}, err
		}*/
	//cxt, cancel := context.WithTimeout(context.Background(), time.Second)
	/*	cxt, cancel := context.WithTimeout(context.Background(), time.Minute)
		defer cancel()*/
	userPd, err = security.Md5(userPd)
	if err != nil {
		return gin.H{"status": http.StatusBadRequest, "message": "could not greet: " + err.Error()}, err
	}

	userform := useraccount.LoginUserFormRequest{UserPhone: userNum, UserPassword: userPd}
	r, err := c.UserRegister(context.Background(), &useraccount.RegisterUserFormRequest{UserForm: &userform, Email: email, Nickname: nickname})
	if err != nil {
		return gin.H{"status": http.StatusBadRequest, "message": "could not greet: " + err.Error()}, err
		//log.Printf("could not greet: %v", err)
	}
	h = gin.H{"status": r.Status, "message": "注册成功"}
	return h, err
}
func UserQueryData(token string) (h gin.H, err error) {
	user2, err := security.VerifyAction(token)
	if err != nil {
		return gin.H{"status": http.StatusBadGateway, "message": err.Error()}, err
	}
	_, ok := security.UserMap[user2.UserID]
	if ok == false {
		return gin.H{"status": http.StatusBadGateway, "message": "账号未登录"}, err
	}
	userId := user2.UserID
	/*	c, err := GetLink()
		if err != nil {
			return gin.H{"status": http.StatusBadRequest, "message": "could not greet: " + err.Error()}, err
		}*/
	//cxt, cancel := context.WithTimeout(context.Background(), time.Second)
	/*	cxt, cancel := context.WithTimeout(context.Background(), time.Minute)
		defer cancel()*/
	data, err := c.UserQueryData(context.Background(), &useraccount.QueryUserFormRequest{UserId: int32(userId)})
	if err != nil {
		return gin.H{"status": http.StatusBadGateway, "message": err.Error()}, err
	}
	h = gin.H{"status": http.StatusOK, "message": "查询成功", "data": data}
	return h, err
}
func UserUpdate(json user.UpdateJson) (h gin.H, err error) {
	userinfo, err := security.VerifyAction(json.Token)
	if err != nil {
		return gin.H{"status": http.StatusBadRequest, "message": "could not greet: " + err.Error()}, err
	}
	_, ok := security.UserMap[userinfo.UserID]
	if ok == false {
		return gin.H{"status": http.StatusBadGateway, "message": "账号未登录"}, err
	}
	err = form_check.CheckData(json)
	if err != nil {
		return gin.H{"status": http.StatusBadRequest, "message": "could not greet: " + err.Error()}, err
	}
	/*	c, err := GetLink()
		if err != nil {
			return gin.H{"status": http.StatusBadRequest, "message": "could not greet: " + err.Error()}, err
		}*/
	/*	cxt, cancel := context.WithTimeout(context.Background(), time.Minute)
		defer cancel()*/
	user2 := useraccount.QueryUserFormResponse{}
	if json.NewPhone != "" {
		user2.UserPhone = json.NewPhone
	}
	if json.NewPassword1 != "" {
		user2.UserPassword = json.NewPassword1
	}
	if json.Email != "" {
		user2.Email = json.Email
	}
	if json.NickName != "" {
		user2.Nickname = json.NickName
	}
	user2.UserId = int32(userinfo.UserID)
	user2.UserPassword, err = security.Md5(user2.UserPassword)
	if err != nil {
		return gin.H{"status": http.StatusBadRequest, "message": err.Error()}, err
	}
	data, err := c.UserUpdate(context.Background(), &useraccount.UpdateUserFormRequest{UserForm: &user2, UserPhone: json.Phone})
	if err != nil {
		return gin.H{"status": http.StatusBadRequest, "message": err.Error()}, err
	}

	h = gin.H{"status": data.Status, "message": "修改成功!"}
	//_,ok:=security.UserMap[userinfo.UserID]
	delete(security.UserMap, userinfo.UserID)
	return h, err
}
